package net.example.model.contract;

import com.oraycn.omcs.utils.BufferUtils;
import com.oraycn.omcs.utils.SerializeUtils;

import net.example.utils.SerializeHelper;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import io.netty.buffer.ByteBuf;

public class HandleAddGroupRequestContract
{
    public String RequesterID ;

    public String GroupID ;

    public boolean IsAgreed ;

    public HandleAddGroupRequestContract(){}

    public HandleAddGroupRequestContract(String requesterID, String groupID, boolean agree)
    {
        this.RequesterID = requesterID;
        this.GroupID = groupID;
        this.IsAgreed = agree;
    }

    public byte[] serialize() throws Exception {
        byte[] bGroupID=GroupID.getBytes(StandardCharsets.UTF_8);
        byte[] bRequesterID = RequesterID.getBytes(StandardCharsets.UTF_8);

        int contractLen = 4 + 4 +bGroupID.length+ 1 +4 +bRequesterID.length;
        ByteBuf body = BufferUtils.newBuffer();
        body.writeInt(contractLen);
        body.writeInt(bGroupID.length);
        body.writeBytes(bGroupID);
        body.writeBoolean(this.IsAgreed);
        body.writeInt(bRequesterID.length);
        body.writeBytes(bRequesterID);
        byte[] result = new byte[body.readableBytes()];
        System.arraycopy(body.array(),0,result,0,result.length);
        return result;
    }

    public void deserialize(byte[] info) throws IOException
    {
        ByteBuf buffer = SerializeHelper.wrappedBuffer(info);
        buffer.readInt();
        this.GroupID = SerializeUtils.readStrIntLen(buffer);
        this.IsAgreed = buffer.readBoolean();
        this.RequesterID = SerializeUtils.readStrIntLen(buffer);
    }
}
